package com.masterclient.app.modules.income.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.income.model.UnClearAccountBean;
import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import jp.wasabeef.glide.transformations.internal.Utils;

public class ClearAccountAdapter extends BaseQuickAdapter<UnClearAccountBean.GoodsBean, BaseViewHolder> {

    private final Context mContext;

    public ClearAccountAdapter(Context context) {
        super(R.layout.item_clear_account);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, UnClearAccountBean.GoodsBean item) {

        ImageView ivHead = helper.getView(R.id.iv_head);
        TextView tvNickName = helper.getView(R.id.tv_nick_name);
        TextView tvStatus = helper.getView(R.id.tv_status);
        TextView tvName = helper.getView(R.id.tv_name);
        TextView tvCreateTime = helper.getView(R.id.tv_create_time);

        View viewSingleImg = helper.getView(R.id.view_single_img);
        ImageView image = helper.getView(R.id.image);
        TextView tvTitle = helper.getView(R.id.tv_title);

        View viewMoreImg = helper.getView(R.id.view_more_img);
        RecyclerView recyclerView = helper.getView(R.id.recycler);
        TextView tvAccount = helper.getView(R.id.tv_account);

        ViewTransformUtil.glideImageView(mContext, item.headImgUrl, ivHead,
                new CropCircleWithBorderTransformation(Utils.toDp(2), 0xFFFFFF), R.mipmap.user_head_default_icon);
        tvNickName.setText(item.showName);
        tvName.setText(item.orderType);
        tvCreateTime.setText(item.orderTime);
        if (!TextUtils.isEmpty(item.refundDesc)) {
            tvStatus.setVisibility(View.VISIBLE);
            tvStatus.setText(item.refundDesc);
        }else {
            tvStatus.setVisibility(View.GONE);
        }

        if (item.orderImage != null && item.orderImage.size() == 1) {
            viewSingleImg.setVisibility(View.VISIBLE);
            viewMoreImg.setVisibility(View.GONE);

            ViewTransformUtil.glideImageView(mContext, item.orderImage.get(0).image, image,
                    new RoundedCornersTransformation(ScreenUtils.dp2px(3), 0),
                    R.mipmap.defaults, ScreenUtils.dp2px(54), ScreenUtils.dp2px(54));
            tvTitle.setText(item.orderImage.get(0).name);
        } else {
            viewSingleImg.setVisibility(View.GONE);
            viewMoreImg.setVisibility(View.VISIBLE);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            ClearAccountImageAdapter adapter = new ClearAccountImageAdapter(mContext);
            recyclerView.setAdapter(adapter);
            recyclerView.setOnTouchListener((v, event) -> helper.itemView.onTouchEvent(event));
            adapter.bindToRecyclerView(recyclerView);
            adapter.setNewData(item.orderImage);
        }
        tvAccount.setText(item.amount);
    }
}
